package com.nativetalk.controller.app.share;

import com.nativetalk.base.RetInfo;
import com.nativetalk.bean.member.TdNtMemberRegister;
import com.nativetalk.bean.sms.Sms;
import com.nativetalk.bean.website.TsNtWebSiteConfIndex;
import com.nativetalk.common.Constants;
import com.nativetalk.common.ValidationMessages;
import com.nativetalk.common.util.JsonUtil;
import com.nativetalk.common.util.MemcachedKey;
import com.nativetalk.common.util.MemcachedUtils;
import com.nativetalk.service.member.register.MemberRegisterService;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.List;
import java.util.Map;

/**
 * Created by lizhun on 15/12/9.
 */
@Controller
public class ShareController {
    private static Logger logger = Logger.getLogger(ShareController.class);

    @Resource
    private MemberRegisterService memberRegisterService;

    /**
     * @return RetInfo
     * @Purpose 分享注册
     * @version 2.0
     * @time 2015-12-28
     * @author lizhun
     */
    @RequestMapping(value = "/share", method = {RequestMethod.GET, RequestMethod.POST})
    public ModelAndView register(@RequestParam(value = "invite_code", required = false) String invite_code,
                                 HttpServletRequest request) {
        ModelAndView mv = new ModelAndView("/app/share/register");
        try {
            //打印入参
            logger.info(Constants.prefix_in + "invite_code=" + invite_code);
            Map<String, TsNtWebSiteConfIndex> tsNtWebSiteConfIndexMap = (Map<String, TsNtWebSiteConfIndex>) MemcachedUtils.get(MemcachedKey.tsNtWebSiteConfIndexMap);
            mv.addObject("computer_download_url",tsNtWebSiteConfIndexMap.get("31").getData_value());
            mv.addObject("iOS_download_url",tsNtWebSiteConfIndexMap.get("32").getData_value());
            mv.addObject("Android_download_url",tsNtWebSiteConfIndexMap.get("33").getData_value());
            mv.addObject("invite_code", invite_code);

            //打印出参
            logger.info(Constants.prefix_out + "invite_code=" + invite_code);
        } catch (Exception e) {
            //记录错误日志
            RetInfo retInfo = RetInfo.getServerErrorInfo();
            logger.error(e.getMessage(), e);

            //打印出参
            logger.error(Constants.prefix_out + JsonUtil.toJson(retInfo));
        }
        return mv;
    }


    /**
     * @param sms
     * @return RetInfo
     * @Purpose 分享注册发送短信
     * @version 2.0
     * @time 2015-12-22
     * @author lizhun
     */
    @RequestMapping(value = "/share/shareRegisterSms", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public RetInfo shareRegisterSms(@Valid @ModelAttribute Sms sms, BindingResult result) {

        try {
            //打印入参
            logger.info(Constants.prefix_in + JsonUtil.toJson(sms));

            RetInfo retInfo = new RetInfo();
            if (result.hasErrors()) {
                List<FieldError> err = result.getFieldErrors();
                FieldError fe = err.get(0);
                retInfo.setMark(ValidationMessages.getValue("MARKPARAMETERERROR"));
                retInfo.setTip(fe.getDefaultMessage());
            } else {
                retInfo = memberRegisterService.smsRegister(sms);
            }

            //打印出参
            logger.info(Constants.prefix_out + JsonUtil.toJson(retInfo));
            return retInfo;
        } catch (Exception e) {
            //记录错误日志
            RetInfo retInfo = RetInfo.getServerErrorInfo();
            logger.error(e.getMessage(), e);

            //打印出参
            logger.error(Constants.prefix_out + JsonUtil.toJson(retInfo));
            return retInfo;
        }
    }

    /**
     * @param tdNtMemberRegister
     * @return RetInfo
     * @Purpose 注册
     * @version 1.0
     * @time 2015-12-22
     * @author lizhun
     */
    @RequestMapping(value = "/share/register", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public RetInfo register(@Valid @ModelAttribute TdNtMemberRegister tdNtMemberRegister, BindingResult result) {
        try {
            //打印入参
            logger.info(Constants.prefix_in + JsonUtil.toJson(tdNtMemberRegister));

            RetInfo retInfo = new RetInfo();
            if (result.hasErrors()) {
                List<FieldError> err = result.getFieldErrors();
                FieldError fe = err.get(0);
                retInfo.setMark(ValidationMessages.getValue("MARKPARAMETERERROR"));
                retInfo.setTip(fe.getDefaultMessage());
            } else {
                tdNtMemberRegister.setEqu_type("3");
                retInfo = memberRegisterService.register(tdNtMemberRegister, 1);
            }

            //打印出参
            logger.info(Constants.prefix_out + JsonUtil.toJson(retInfo));
            return retInfo;
        } catch (Exception e) {
            //记录错误日志
            RetInfo retInfo = RetInfo.getServerErrorInfo();
            logger.error(e.getMessage(), e);

            //打印出参
            logger.error(Constants.prefix_out + JsonUtil.toJson(retInfo));
            return retInfo;
        }
    }

    /**
     * @return RetInfo
     * @Purpose 商家分享注册
     * @version 2.0
     * @time 2015-12-28
     * @author lizhun
     */
    @RequestMapping(value = "/cooperation ", method = {RequestMethod.GET, RequestMethod.POST})
    public ModelAndView cooperation(@RequestParam(value = "cooperation", required = false) String cooperation,
                                    HttpServletRequest request) {
        ModelAndView mv = new ModelAndView("/app/cooperation/register");
        try {
            //打印入参
            logger.info(Constants.prefix_in + "cooperation=" + cooperation);

            mv.addObject("invite_code", cooperation);

            //打印出参
            logger.info(Constants.prefix_out + "cooperation=" + cooperation);
        } catch (Exception e) {
            //记录错误日志
            RetInfo retInfo = RetInfo.getServerErrorInfo();
            logger.error(e.getMessage(), e);

            //打印出参
            logger.error(Constants.prefix_out + JsonUtil.toJson(retInfo));
        }
        return mv;
    }

    /**
     * @param tdNtMemberRegister
     * @return RetInfo
     * @Purpose 商家分享注册
     * @version 1.0
     * @time 2015-12-22
     * @author lizhun
     */
    @RequestMapping(value = "/cooperation/register", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public RetInfo cregister(@Valid @ModelAttribute TdNtMemberRegister tdNtMemberRegister, BindingResult result) {
        try {
            //打印入参
            logger.info(Constants.prefix_in + JsonUtil.toJson(tdNtMemberRegister));

            RetInfo retInfo = new RetInfo();
            if (result.hasErrors()) {
                List<FieldError> err = result.getFieldErrors();
                FieldError fe = err.get(0);
                retInfo.setMark(ValidationMessages.getValue("MARKPARAMETERERROR"));
                retInfo.setTip(fe.getDefaultMessage());
            } else {
                tdNtMemberRegister.setEqu_type("4");// 4是什么鬼. fuck!..
                retInfo = memberRegisterService.registerCoopersion(tdNtMemberRegister);
            }

            //打印出参
            logger.info(Constants.prefix_out + JsonUtil.toJson(retInfo));
            return retInfo;
        } catch (Exception e) {
            //记录错误日志
            RetInfo retInfo = RetInfo.getServerErrorInfo();
            logger.error(e.getMessage(), e);

            //打印出参
            logger.error(Constants.prefix_out + JsonUtil.toJson(retInfo));
            return retInfo;
        }
    }

}


